package com.hl.mapper;

import com.hl.pojo.Course;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CourseMapper {

    /**
     * 向数据库中插入课程信息
     *
     * @param course 课程对象，包含要插入的数据
     * @return 插入的行数
     */
    @Insert("INSERT INTO course (course_name, course_duration, courseware_count, course_intro, target_audience, learning_outcomes, subscription, read_count, consultant_id,money,image) VALUES (#{courseName}, #{courseDuration}, #{coursewareCount}, #{courseIntro}, #{targetAudience}, #{learningOutcomes}, #{subscription}, #{readCount}, #{consultantId},#{money},#{image})")
    Integer addCourse(Course course);

    @Update("UPDATE course SET course_name=#{courseName}, course_duration=#{courseDuration}, courseware_count=#{coursewareCount}, course_intro=#{courseIntro}, target_audience=#{targetAudience}, learning_outcomes=#{learningOutcomes}, subscription=#{subscription}, read_count=#{readCount}, consultant_id=#{consultantId},money=#{money},image=#{image} WHERE id=#{id}")
    Integer updateCourse(Course course);

    /**
     * 删除指定ID的课程
     *
     * @param id 要删除的课程ID
     * @return 删除的课程数量
     */
    @Update("update course set del = 1 WHERE id =#{id}")
    Integer deleteCourse(Integer id);

    /**
     * 根据课程ID查询课程信息
     *
     * @param id 课程ID
     * @return 返回与给定ID匹配的课程对象，如果未找到则返回null
     */
    @Select("SELECT * FROM course WHERE id =#{id}")
    @Results({
            @Result(property = "consultant",column = "consultant_id",
                    one = @One(select = "com.hl.mapper.ConsultantMapper.findById")
            )
    })
    Course findCourseById(Integer id);

    /**
     * 查询所有课程信息
     *
     * @return 包含所有课程信息的列表
     */
    @Select("SELECT * FROM course where del = 0")
    List<Course> findAllCourses();

    @Select("SELECT * FROM course where del = 0 and course_name like '%${courseName}%'")
    List<Course> findByCourseName(String courseName);
}